package vincent.com.layouttest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.widget.DrawerLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;

import vincent.com.layouttest.R;

/**
 * Created by zhengwenchen on 2016/4/22.
 */
public class MyDrawerLayout extends DrawerLayout {

    private int mColor;

    private Drawable mColorDrawable;

    private int mStatusBarHeight;

    public MyDrawerLayout(Context context) {
        this(context, null);
    }

    public MyDrawerLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedValue tv = new TypedValue();
        getContext().getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
        mColor = tv.data;

        mColorDrawable = new ColorDrawable(mColor);

        mStatusBarHeight = getStatusBarHeight();
    }

    @Override
    public void onDraw(Canvas c) {
        super.onDraw(c);

        int mWidth = getWidth();
        Rect rect = new Rect(0, 0, mWidth, mStatusBarHeight);

        Log.i("czw_drawerlayout", "mWidth: " + mWidth + " | rect: " + rect.left + ", " + rect.top + ", "
                + rect.right + ", " + rect.bottom + " | color: " + mColor);

        int count = c.save();
        mColorDrawable.setBounds(rect);
        mColorDrawable.draw(c);
        c.restoreToCount(count);
    }

    public int getStatusBarHeight(){
        int height = 0;
        int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        height = getResources().getDimensionPixelSize(resId);

        return height;
    }
}
